package com.aplink.generic.xmlpullparser;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.text.TextUtils;
import android.util.Xml;

import com.aplink.generic.pojo.UpdateInfoList;
import com.aplink.generic.pojo.UpdateInfoRecord;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class UpgradeDatabaseXmlPullParser {
	private static final String ATRRIBUTE = "attribute";
	private static final String DROP_COLUMN = "dropColumn";
	private static final String DROP_TABLE = "dropTable";
	private static final String ID = "id";
	private static String mNameSpace = null;
	private static final String RECORD = "record";
	private static final String TABLE = "table";
	private static final String TABLE_DATA = "tables_data";

	private static final String VALUE = "value";

	public static UpdateInfoList parse(int xmlFile, Context context) {
		InputStream inputStream = XmlPaserUtil.getXMLInputStream(xmlFile,
		        context);
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(inputStream, null);
			parser.nextTag();
			return read(parser);
		} catch (XmlPullParserException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new UpdateInfoList();
	}

	public static UpdateInfoList parse(String xmlFile, Context context) {
		int fileName = ResourcesHelper.getRawIdByName(xmlFile);
		if (fileName != ResourcesHelper.RESOURCE_NOT_FOUND) {
			return parse(fileName, context);
		}
		return new UpdateInfoList();
	}

	private static UpdateInfoList read(XmlPullParser pullParser)
	        throws XmlPullParserException, IOException {
		UpdateInfoList updateInfoList = null;
		pullParser.require(XmlPullParser.START_TAG, mNameSpace, TABLE_DATA);
		while (pullParser.next() != XmlPullParser.END_TAG) {
			if (pullParser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			if (pullParser.getName().equalsIgnoreCase(TABLE)) {
				updateInfoList = readTable(pullParser);
			}
		}
		return updateInfoList;
	}

	private static void readAtrribute(XmlPullParser pullParser,
	        UpdateInfoRecord updateInfoRecord) throws XmlPullParserException,
	        IOException {
		pullParser.require(XmlPullParser.START_TAG, mNameSpace, ATRRIBUTE);
		if (pullParser.getAttributeValue(mNameSpace, ID).equalsIgnoreCase(
		        "TableName")) {
			updateInfoRecord.setTableName(pullParser.getAttributeValue(
			        mNameSpace, VALUE));
		} else if (pullParser.getAttributeValue(mNameSpace, ID)
		        .equalsIgnoreCase("Column")) {
			updateInfoRecord.setColumn(pullParser.getAttributeValue(mNameSpace,
			        VALUE));
		} else if (pullParser.getAttributeValue(mNameSpace, ID)
		        .equalsIgnoreCase("ColumnType")) {
			updateInfoRecord.setColumnType(pullParser.getAttributeValue(
			        mNameSpace, VALUE));
		} else if (pullParser.getAttributeValue(mNameSpace, ID)
		        .equalsIgnoreCase("Version")) {
			updateInfoRecord.setVersion(Integer.parseInt(pullParser
			        .getAttributeValue(mNameSpace, VALUE)));
		}
		pullParser.nextTag();
		pullParser.require(XmlPullParser.END_TAG, mNameSpace, ATRRIBUTE);
	}

	private static void readRecord(XmlPullParser pullParser,
	        UpdateInfoList updateInfoList) throws XmlPullParserException,
	        IOException {
		pullParser.require(XmlPullParser.START_TAG, mNameSpace, RECORD);
		UpdateInfoRecord updateInfoRecord = new UpdateInfoRecord();
		String attributeValue = pullParser.getAttributeValue(mNameSpace,
		        DROP_COLUMN);
		if (!TextUtils.isEmpty(attributeValue)) {
			updateInfoRecord
			        .setDropColumn(attributeValue.equalsIgnoreCase("Y"));
		}
		attributeValue = pullParser.getAttributeValue(mNameSpace, DROP_TABLE);
		if (!TextUtils.isEmpty(attributeValue)) {
			updateInfoRecord.setDropTable(attributeValue.equalsIgnoreCase("Y"));
		}
		while (pullParser.next() != XmlPullParser.END_TAG) {
			if (pullParser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			if (pullParser.getName().equalsIgnoreCase(ATRRIBUTE)) {
				readAtrribute(pullParser, updateInfoRecord);
			}
		}
		updateInfoList.add(updateInfoRecord.getVersion(), updateInfoRecord);
	}

	private static UpdateInfoList readTable(XmlPullParser pullParser)
	        throws XmlPullParserException, IOException {
		UpdateInfoList updateInfoList = new UpdateInfoList();
		pullParser.require(XmlPullParser.START_TAG, mNameSpace, TABLE);
		updateInfoList.setTableID(pullParser.getAttributeValue(mNameSpace, ID));
		while (pullParser.next() != XmlPullParser.END_TAG) {
			if (pullParser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			if (pullParser.getName().equalsIgnoreCase(RECORD)) {
				readRecord(pullParser, updateInfoList);
			}
		}
		return updateInfoList;
	}
}
